/*
 * 文件名：UserServiceImpl.java
 * 版权：Copyright by will_awoke
 * 描述：
 * 修改人：will_awoke
 * 修改时间：2014-8-18
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.qbgs.rbac.service.impl;


import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.qbgs.base.BaseResult;
import com.qbgs.base.Page;
import com.qbgs.constant.GlobalConstant;
import com.qbgs.rbac.dao.RoleDao;
import com.qbgs.rbac.dao.UserDao;
import com.qbgs.rbac.service.UserService;


/**
 * 
 * @author will_awoke
 * @version 2014-8-18
 * @see UserServiceImpl
 * @since
 */
@Service(value = "userService")
public class UserServiceImpl implements UserService
{

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Override
    public BaseResult queryUserPage(int currentPage, int numPerPage, String loginName)
    {
        Page page = userDao.queryUserPage(currentPage, numPerPage, loginName);

        // 将easyui分页有关属性、页面操作提示语 存入baseResult
        BaseResult result = new BaseResult(page.getTotal(), page.getRows());
        if (page.getTotal() == 0)
        {
            // 未查询到记录时，添加相应提示语
            result.setSuccess(false);
            result.setOpMsg(GlobalConstant.QUERRY_FAILED);
        }
        return result;
    }

    @Override
    public boolean updateUserPwd(String loginName, String newPwd)
    {
        return userDao.updateUserPwd(loginName, newPwd) > 0;
    }

    //事务控制
    @Override
    @Transactional
    public boolean addUserOfRole(String loginName, String password, String realName, String email,
                                 String state, String remarks, String roleId)
    {
        //1. 获得新增用户的主键
        String userId = String.valueOf(userDao.addUser(loginName, password, realName, email,
            state, remarks));
        if (StringUtils.isEmpty(userId))
        {
            return false;
        }
        //2. 插入用户角色关联
        for (String role : StringUtils.split(roleId, ","))
        {
            userDao.addUserRole(userId, role);
        }
        return true;
    }

    @Override
    public boolean judgeLoginNameUnique(String loginName)
    {
        return userDao.queryByLoginName(loginName) > 0;
    }

    @Override
    public String queryRoleIdsOfUser(String userId)
    {
        //查询用户所绑定角色id集合
        List<String> roleIds = userDao.queryRoleIdsByUserId(userId);
        StringBuilder temp = new StringBuilder("");
        for (String roleId : roleIds)
        {
            temp.append(roleId).append(",");
        }
        //截取掉最后一个逗号
        int length = temp.length();
        if (length > 0)
        {
            return temp.substring(0, length - 1);
        }
        return "";
    }

    @Override
    public String queryRoleNamesOfUser(String userId)
    {
        //查询用户所绑定角色id集合
        List<String> roleIds = userDao.queryRoleIdsByUserId(userId);
        StringBuilder roleNames = new StringBuilder("");
        for (String roleId : roleIds)
        {
            List<Map<String, Object>> map = roleDao.queryRoleById(roleId);
            //确保角色id对应的角色存在
            if (!CollectionUtils.isEmpty(map))
            {
                roleNames.append(String.valueOf(map.get(0).get("ROLE_NAME"))).append(",");
            }
        }
        //截取掉最后一个逗号
        int length = roleNames.length();
        if (length > 0)
        {
            return roleNames.substring(0, length - 1);
        }
        return "";
    }

    @Override
    public boolean deleteUser(String userId)
    {
        return userDao.deleteUser(userId) > 0;
    }

    //事务控制
    @Override
    @Transactional
    public boolean updateUserOfRole(String userId, String loginName, String realName, String email,
                                 String remarks, String roleId)
    {
        //1. 修改用户的信息
        userDao.updateUser(userId, loginName, realName, email, remarks);

        //2. 删除用户现有角色绑定关系
        userDao.deleteRolesOfUser(userId);

        //3. 插入新的用户角色关联
        for (String role : StringUtils.split(roleId, ","))
        {
            userDao.addUserRole(userId, role);
        }
        return true;
    }

}
